<template>
  <div class="template-market-page">
    <div class="page-header">
      <h2>模板广场</h2>
      <div class="header-actions">
        <el-button type="primary" icon="Upload">上传模板</el-button>
        <el-input
          v-model="searchKeyword"
          placeholder="搜索模板"
          prefix-icon="Search"
          style="width: 300px"
          clearable
        />
      </div>
    </div>

    <!-- 筛选栏 -->
    <el-card class="filter-card">
      <el-space wrap>
        <el-tag
          v-for="category in categories"
          :key="category.value"
          :type="selectedCategory === category.value ? 'primary' : 'info'"
          @click="selectCategory(category.value)"
          style="cursor: pointer"
        >
          {{ category.label }}
        </el-tag>
      </el-space>
      <el-divider direction="vertical" />
      <el-radio-group v-model="sortType" size="small">
        <el-radio-button label="hot">最热</el-radio-button>
        <el-radio-button label="new">最新</el-radio-button>
        <el-radio-button label="download">使用量</el-radio-button>
      </el-radio-group>
    </el-card>

    <!-- 模板列表 -->
    <el-row :gutter="20" class="template-list">
      <el-col :span="8" v-for="template in filteredTemplates" :key="template.id">
        <el-card class="template-card" shadow="hover">
          <div class="template-preview">
            <img :src="template.preview" :alt="template.name" />
            <div class="template-overlay">
              <el-button type="primary" @click="previewTemplate(template)">
                <el-icon><View /></el-icon>
                预览
              </el-button>
              <el-button type="success" @click="useTemplate(template)">
                <el-icon><Download /></el-icon>
                使用
              </el-button>
            </div>
          </div>
          <div class="template-info">
            <div class="template-name">
              {{ template.name }}
              <el-tag v-if="template.badge" type="danger" size="small">{{ template.badge }}</el-tag>
            </div>
            <div class="template-desc">{{ template.description }}</div>
            <div class="template-meta">
              <el-tag :type="getCategoryType(template.category)" size="small">
                {{ getCategoryLabel(template.category) }}
              </el-tag>
              <div class="meta-stats">
                <span><el-icon><Star /></el-icon> {{ template.rating }}</span>
                <span><el-icon><View /></el-icon> {{ template.views }}</span>
                <span><el-icon><Download /></el-icon> {{ template.uses }}</span>
              </div>
            </div>
            <div class="template-tags">
              <el-tag v-for="tag in template.tags" :key="tag" size="small" type="info">
                {{ tag }}
              </el-tag>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 预览对话框 -->
    <el-dialog v-model="showPreviewDialog" :title="selectedTemplate?.name" width="1200px" fullscreen>
      <div v-if="selectedTemplate" class="template-preview-detail">
        <iframe
          :src="selectedTemplate.previewUrl"
          style="width: 100%; height: calc(100vh - 200px); border: none"
        ></iframe>
      </div>
      <template #footer>
        <el-button @click="showPreviewDialog = false">关闭</el-button>
        <el-button type="primary" @click="useTemplate(selectedTemplate)">
          <el-icon><Download /></el-icon>
          使用此模板
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

// 搜索关键词
const searchKeyword = ref('')

// 选中的分类
const selectedCategory = ref('all')

// 排序类型
const sortType = ref('hot')

// 分类
const categories = ref([
  { label: '全部', value: 'all' },
  { label: '管理系统', value: 'admin' },
  { label: '数据大屏', value: 'dashboard' },
  { label: '电商系统', value: 'ecommerce' },
  { label: '移动端', value: 'mobile' },
  { label: '营销活动', value: 'marketing' }
])

// 模板列表
const templateList = ref([
  {
    id: 1,
    name: '通用后台管理模板',
    description: '包含用户管理、角色权限、系统设置等功能的完整后台模板',
    category: 'admin',
    preview: 'https://via.placeholder.com/400x300?text=Admin+Template',
    previewUrl: 'https://preview.example.com/admin',
    rating: 4.9,
    views: 12586,
    uses: 3256,
    tags: ['Vue3', 'Element Plus', '响应式'],
    badge: 'HOT'
  },
  {
    id: 2,
    name: '数据大屏模板',
    description: '炫酷的数据可视化大屏，支持实时数据刷新',
    category: 'dashboard',
    preview: 'https://via.placeholder.com/400x300?text=Big+Screen',
    previewUrl: 'https://preview.example.com/bigscreen',
    rating: 4.8,
    views: 8526,
    uses: 1856,
    tags: ['ECharts', '大屏', '可视化'],
    badge: 'HOT'
  },
  {
    id: 3,
    name: '电商商城模板',
    description: '完整的电商系统，包含商品、订单、会员等功能',
    category: 'ecommerce',
    preview: 'https://via.placeholder.com/400x300?text=E-commerce',
    previewUrl: 'https://preview.example.com/shop',
    rating: 4.7,
    views: 6428,
    uses: 1256,
    tags: ['电商', '购物车', '支付'],
    badge: 'NEW'
  },
  {
    id: 4,
    name: '移动端H5模板',
    description: '精美的移动端页面模板，完美适配各种手机',
    category: 'mobile',
    preview: 'https://via.placeholder.com/400x300?text=Mobile+H5',
    previewUrl: 'https://preview.example.com/mobile',
    rating: 4.6,
    views: 5286,
    uses: 986,
    tags: ['移动端', 'H5', '响应式']
  }
])

// 对话框
const showPreviewDialog = ref(false)
const selectedTemplate = ref(null)

// 筛选后的模板
const filteredTemplates = computed(() => {
  let list = templateList.value

  // 分类筛选
  if (selectedCategory.value !== 'all') {
    list = list.filter(t => t.category === selectedCategory.value)
  }

  // 关键词搜索
  if (searchKeyword.value) {
    list = list.filter(t =>
      t.name.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      t.description.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }

  // 排序
  if (sortType.value === 'hot') {
    list = list.sort((a, b) => b.rating - a.rating)
  } else if (sortType.value === 'new') {
    list = list.sort((a, b) => b.id - a.id)
  } else if (sortType.value === 'download') {
    list = list.sort((a, b) => b.uses - a.uses)
  }

  return list
})

// 选择分类
const selectCategory = (category) => {
  selectedCategory.value = category
}

// 获取分类类型
const getCategoryType = (category) => {
  const types = {
    admin: 'primary',
    dashboard: 'success',
    ecommerce: 'warning',
    mobile: 'info',
    marketing: 'danger'
  }
  return types[category] || 'info'
}

// 获取分类标签
const getCategoryLabel = (category) => {
  const labels = {
    admin: '管理系统',
    dashboard: '数据大屏',
    ecommerce: '电商系统',
    mobile: '移动端',
    marketing: '营销活动'
  }
  return labels[category] || category
}

// 预览模板
const previewTemplate = (template) => {
  selectedTemplate.value = template
  showPreviewDialog.value = true
}

// 使用模板
const useTemplate = (template) => {
  ElMessage.success(`正在应用 ${template.name} 模板...`)
  setTimeout(() => {
    ElMessage.success('模板应用成功！即将跳转到编辑器...')
    setTimeout(() => {
      router.push('/lowcode/page-designer')
    }, 1000)
  }, 1500)
}
</script>

<style scoped lang="scss">
.template-market-page {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  h2 {
    margin: 0;
    font-size: 24px;
    color: #303133;
  }

  .header-actions {
    display: flex;
    gap: 15px;
  }
}

.filter-card {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    display: flex;
    align-items: center;
    gap: 20px;
  }
}

.template-list {
  .template-card {
    margin-bottom: 20px;
    transition: transform 0.3s;

    &:hover {
      transform: translateY(-4px);

      .template-preview .template-overlay {
        opacity: 1;
      }
    }

    .template-preview {
      position: relative;
      width: 100%;
      height: 200px;
      background: #f5f7fa;
      border-radius: 4px;
      overflow: hidden;
      margin-bottom: 15px;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .template-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        opacity: 0;
        transition: opacity 0.3s;
      }
    }

    .template-info {
      .template-name {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 16px;
        font-weight: bold;
        color: #303133;
        margin-bottom: 8px;
      }

      .template-desc {
        font-size: 13px;
        color: #606266;
        margin-bottom: 15px;
        min-height: 40px;
      }

      .template-meta {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 15px;

        .meta-stats {
          display: flex;
          gap: 12px;
          font-size: 12px;
          color: #909399;

          span {
            display: flex;
            align-items: center;
            gap: 4px;
          }
        }
      }

      .template-tags {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
      }
    }
  }
}
</style>

